/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.art.base.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.art.base.util.log.LogUtil;

/**
 * 该类有两种调用方式
 * 第一种: 必须先调用init()方法初始化.  才能调用getInstance()返回对象再调用方法进行操作
 * 第二种: 不用初始化, 但是必须调用getInstance(Context ctx)传入上下文对象获取工具类对象再调用方法进行操作
 */
public class PreferenceUtils {

	/**
	 * 保存Preference的name
	 */
	private static SharedPreferences mSharedPreferences;
	private static PreferenceUtils mPreferenceUtils;
	private static SharedPreferences.Editor editor;
	private static Context context;
	
	private static final String DEFAULT_NAME = "default_name";

	private PreferenceUtils() {
	}
	
	
	//------------------------第一种调用方法------------------------------------
	public static void init(Context ctx){
		context = ctx;
	}
	
	/**
	 * 必须先初始化, 才能调用此方法
	 * @return
	 */
	public static PreferenceUtils getInstance(){
		if (context == null) {
			LogUtil.trace(Log.DEBUG, "PreferenceUtils", "PreferenceUtils 未初始化");
			return null;
		}else {
			return getInstance(context);
		}
	}
	
	/**
	 * 必须先初始化, 才能调用此方法
	 * @param preName	自定义name
	 * @return
	 */
	public static PreferenceUtils getInstance(String preName){
		if (context == null) {
			LogUtil.trace(Log.DEBUG, "PreferenceUtils", "PreferenceUtils 未初始化");
			return null;
		}else {
			return getInstance(context, preName);
		}
	}
	//------------------------------------------------------------
	
	
	
	//----------------------------第二种调用方法--------------------------------
	/**
	 * 单例模式，获取instance实例
	 * 
	 * @param cxt
	 * @return
	 */
	public static PreferenceUtils getInstance(Context cxt) {
		return getInstance(cxt, DEFAULT_NAME);
	}
	
	/**
	 * 单例模式，获取instance实例
	 * 
	 * @param cxt
	 * @param preName 自定义name
	 * @return
	 */
	public static PreferenceUtils getInstance(Context cxt, String preName) {
		context = cxt.getApplicationContext();
		if (mPreferenceUtils == null) {
			mPreferenceUtils = new PreferenceUtils();
		}
		
		mSharedPreferences = cxt.getSharedPreferences(preName,
				Context.MODE_PRIVATE);
		
		editor = mSharedPreferences.edit();
		
		return mPreferenceUtils;
	}
	//------------------------------------------------------------
	
	
	
	/**
	 * 保存
	 * @param value
	 * @param key
	 */
	public void save(String key, String value){
		editor.putString(key, value);
		editor.commit();
	}
	public void save(String key, boolean value){
		editor.putBoolean(key, value);
		editor.commit();
	}
	public void save(String key, int value){
		editor.putInt(key, value);
		editor.commit();
	}

	public void save(String key, long value){
		editor.putLong(key, value);
		editor.commit();
	}
	
	/**
	 * 获取
	 * @param key
	 * @return
	 */
	public String getString(String key){
		return getString(key, "");
	}
	public String getString(String key, String defaultValue){
		return mSharedPreferences.getString(key, defaultValue);
	}
	/**
	 * 获取失败,则默认返回false
	 */
	public boolean getBoolean(String key){
		return getBoolean(key, false);
	}
	public boolean getBoolean(String key, boolean defaultValue){
		return mSharedPreferences.getBoolean(key, defaultValue);
	}
	/**
	 * 获取失败,默认返回0
	 */
	public int getInt(String key){
		return getInt(key, 0);
	}
	public int getInt(String key, int defaultValue){
		return mSharedPreferences.getInt(key, defaultValue);
	}

	/**
	 * 获取失败,默认返回0
	 */
	public long getLong(String key){
		return getLong(key, 0l);
	}
	public long getLong(String key, long defaultValue){
		return mSharedPreferences.getLong(key, defaultValue);
	}
	
	//
	// /**
	// * 设置当前用户
	// */
	// public void setCurrentUser(String userName) {
	// editor.putString(SHARED_KEY_SETTTING_USER_CURRENT, userName);
	// editor.commit();
	// }
	//
	// /**
	// * 获取当前用户
	// *
	// * @param userName
	// * @return
	// */
	// public String getCurrentUser() {
	// return mSharedPreferences.getString(SHARED_KEY_SETTTING_USER_CURRENT,
	// "");
	// }
}
